/************************************************************************************
* ArtistModel.cpp                                                                        *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                              *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/ 

#include "ArtistModel.h"



ArtistModel::ArtistModel(QObject *parent)
    :QAbstractItemModel(parent) {
    m_artists = Collector::get()->allArtists();

}


int ArtistModel::rowCount(const QModelIndex &parent) const {
    return m_artists.size();
}


int ArtistModel::columnCount(const QModelIndex &parent) const {
    return 2;
}


QVariant ArtistModel::data(const QModelIndex &index, int role) const {
    if(index.isValid()) {
        if(role == Qt::DisplayRole) {
            ArtistInfo *artist = m_artists.at(index.row());
            switch(index.column()) {
            case 0: return artist->m_artistName.isEmpty()?NOT_AVAILABLE:artist->m_artistName;
            case 1: return artist->m_albums->size();
//            case 2: return getArtistAvgRating(artist);
            }
        }
        else if(role == Qt::SizeHintRole) {
              return QSize(0, 18);
        }
        else if(role == Qt::FontRole) {
            return QFont("Droid Sans", 9);
        }
    }
    return QVariant();
}






QVariant ArtistModel::headerData(int section, Qt::Orientation, int role) const {
    if (role == Qt::TextAlignmentRole) {
        return int (Qt::AlignLeft | Qt::AlignVCenter);
    }
    else if (role == Qt::DisplayRole) {
        switch(section) {
        case 0: return tr("Artist");
        case 1: return tr("No. Albums");
//        case 2: return tr("Avg. Rating");
        }
    }
    return QVariant();
}



QModelIndex ArtistModel::index(int row, int column, const QModelIndex &parent) const {
    return createIndex(row, column);
}


QModelIndex ArtistModel::parent(const QModelIndex &child) const {
    return QModelIndex();
}



bool ArtistModel::hasChildren(const QModelIndex &parent) const {
    if(! parent.isValid()) {
        return true;
    }
    return false;
}



//float ArtistModel::getArtistAvgRating(ArtistInfo *artist) const {
//    float rating = 0;
//    for(QHash<QString, AlbumInfo *>::iterator it = artist->m_albums->begin();
//        it != artist->m_albums->end();
//        ++ it) {
//        AlbumInfo *album = it.value();
//        rating += album->m_avgRating;
//    }
//    return rating / artist->m_albums->size();
//}


void ArtistModel::setModel(QList<ArtistInfo *> model) {
    beginResetModel();
    m_artists = model;
    endResetModel();
}



bool ArtistSortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
//    PartialCollectionModel *model =  (PartialCollectionModel *) sourceModel();
    QString artist = sourceModel()->data(sourceModel()->index(source_row, 0)).toString();
    return artist.contains(m_regExp, Qt::CaseInsensitive);
}
